package com.feifei.service.impl;

import com.feifei.bean.Product;
import com.feifei.bean.ProductExample;
import com.feifei.mapper.ProductMapper;
import com.feifei.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<Product> getAllProducts() {
        List<Product> list = productMapper.selectByExample(null);
        return list;
    }

    @Override
    public Product getProductById(Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    public int addProduct(Product product) {
        productMapper.insert(product);
        // 生成的ID将在 "Product" 对象的 "id" 属性中可用

        // 返回生成的ID
        return product.getId();

    }

    @Override
    public List<Product> getProductByTypeId(Integer typeId) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andTypeIdEqualTo(typeId);
        List<Product> productList = productMapper.selectByExample(productExample);

        return productList;
    }

    @Override
    public List<Product> getProductByStatus(Integer status) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andStatusEqualTo(status);
        List<Product> productList = productMapper.selectByExample(productExample);

        return productList;
    }

    @Override
    public List<Product> getMyProduct(Integer id) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andUserIdEqualTo(id);
        List<Product> productList = productMapper.selectByExample(productExample);
        return productList;
    }

    @Override
    public int deleteMyProduct(Integer id) {
        int i = productMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int editProduct(Product product) {
//        根据主键，选择性地更新数据库中对应的记录。只会更新非空的字段。
        int i = productMapper.updateByPrimaryKeySelective(product);
        return i;
    }
}
